//
   Created by Administrator on 2015/11/7 0007.

extends ./layerout-pc

block style
    link(href="umeditor/themes/default/css/umeditor.css", type="text/css", rel="stylesheet")
    style.

        .add-cotent {
            border: 1px solid #ddd;
            margin-bottom: 40px;
        }

        .title {
            margin: 0 -12px;
            padding: 16px 24px;
            border-bottom: 1px solid #ddd;
            font-size: 18px;
        }

        .add-cotent .head {
            margin: 0 -12px;
            padding-top: 18px !important;
            padding-bottom: 18px !important;
            font-size: 15px;
        }

        .right {
            border-left: 1px solid #ddd;
        }

        .box {
            width: 100%;
            padding: 8px;
            border: 2px solid #43b548;
            text-align: center;
        }

        .add-content {
            position: relative;
            line-height: 1em;
            text-align: center;
            font-size: 80px;
            border: 2px dashed #ddd;
            border-top: 0;
            color: #bbb;
        }

        .add-content .add {
            position: absolute;
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            z-index: 10;
            opacity: 0;
        }

        .box span {
            display: block;
            background: #ccc;
            line-height: 8em;
        }

        .box img {
            width: 100%;
        }

        .select-wrapper ul{
            z-index: 222222;
        }

        .video-content{
            padding: .4em 1.6em !important;
        }

        .sign-out-btn {
            margin-top: 1em;
        }
block content

    .container
        .row
            button.right.col.s2.sign-out-btn.btn.waves-effect.waves-light(href="/sign-out") 退出
        .row
            .add-cotent.col.s12
                .title 新建图文消息
                .left.col.s3.waves-effect.waves-light
                    .col.s12.head 添加封面
                    .col.s12
                        .box
                            img#cover.cover(style="display:none", src=video ? "/r/" + video.cover : "", data-cover=video ? + video.cover : "")
                            span 封面图片
                        .add-content
                            span +
                            input.add(type="file")
                .right.col.s9.video-content
                    .col.s12
                        .row
                        .row
                            input#id(type="hidden", value=video ? video._id : "")
                            .input-field.col.s6
                                input#title.validate(type="text", name="video[title]", value=video ? video.title : "")
                                label(for="title") 标题
                            .input-field.col.s6
                                input#abstract.validate(type="text", name="video[abstract]", value=video ? video.abstract : "")
                                label(for="abstract") 摘要
                            .col.s12
                                if group
                                    label 选择视频组
                                    select#groupid
                                        each value, key in group
                                            if video
                                                option(value=key, selected=video.groupid == key ? true : false)=value
                                            else
                                                option(value=key)=value
                            .row.col.s12
                                label(for="content") 正文
                                script(type="text/plain" id="myEditor" style="width:100%;height:300px;")
                                    p!= video ? video.content : "正文"
                            .row
                        .row
                            button.col.s12.btn.waves-effect.waves-light.save-video-btn 提交
block script

    script(type="text/javascript" charset="utf-8" src="umeditor/umeditor.config.js")
    script(type="text/javascript" charset="utf-8" src="umeditor/umeditor.min.js")
    script(type="text/javascript" src="umeditor/lang/zh-cn/zh-cn.js")

    script.
        $('select').material_select();

        var um = UM.getEditor('myEditor');


        $signout = $('.sign-out-btn')
        $signout.on("click", function () {
            $.ajax("/sign-out", {
                type: "POST",
                data: "",
                contentType: "application/json"
            }).then(function (r) {
                location.href = "/sign-in"
            })
        })

        $save = $('.save-video-btn');
        $save.on("click", function () {
            var time = 2000
            var cover = $("#cover").attr("data-cover");
            if (!cover) {
                Materialize.toast('封面不能为空', time)
                return
            }

            var title = $("#title").val().trim();
            if (!title) {
                Materialize.toast('标题不能为空', time)
                return
            }

            var id = $("#id").val()

            var data = {
                video: {
                    id: id,
                    title: title,
                    abstract: $("#abstract").val().trim(),
                    cover: cover,
                    content: UM.getEditor('myEditor').getContent(),
                    groupid: $("#groupid").val()
                }
            }
            $.ajax("/manage-video", {
                type: "POST",
                data: JSON.stringify(data),
                contentType: "application/json"
            }).then(function (r) {
                if (r.message && r.message === "signin") {
                    location.href = "/sign-in";
                } else {
                    Materialize.toast('添加成功', time)
                    setTimeout(function () {
                        location.reload();
                    }, 2000)
                }
            })
        })

        // add
        $('.add').on('change', function () {
            var files = this.files;
            var file = files[0];
            var xhr = new XMLHttpRequest();

            xhr.open("POST", "/upload5");
            data = new FormData();
            data.append("f0", file);
            xhr.send(data);
            xhr.onload = function (e) {
                xhr = e.currentTarget;
                if (xhr.status != 200) {
                    alert("上传失败!");
                } else {
                    var response = xhr.responseText && JSON.parse(xhr.responseText)
                    for (var f in response) {
                        var path = response[f].path;
                        $(".cover").attr("data-cover", path).attr("src", "/r/" + path).show();
                    }
                    $(".box span").hide()
                }
            }
        })